package com.rw.admin.utils;

import com.rw.common.pojo.Menu;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TreeUtlis {
    // 封装集合为树形结构
    public static List<Menu> buildTree(List<Menu> nodes) {
        Map<Integer, List<Menu>> children = nodes.stream().filter(node -> node.getPid() != 0)
                .collect(Collectors.groupingBy(Menu::getPid));
        nodes.forEach(node -> node.setChild(children.get(node.getId())));
        return nodes.stream().filter(node -> node.getPid() == 0).collect(Collectors.toList());
    }
}
